package cn.codingjc.consumer.intercepter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 请求拦截器
 *
 * @author shenjicheng
 * @date 2022/7/3 12:33 PM
 */

@Slf4j
@Component
public class MicroServiceHTTPIntercepter implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        log.info("【HTTP请求拦截】服务主机：{}, REST路径：{}", request.getURI().getHost(), request.getURI().getPath());
        request.getHeaders().set("token", "test_token");
        return execution.execute(request, body);
    }
}
